package com.ajk.server.dao.provider;

import java.text.MessageFormat;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;

import com.ajk.server.model.CreditsHistory;
import com.ajk.server.model.query.CreditsHistoryQuery;
import com.ajk.server.utils.IntegerUtil;
/**
 * 积分历史  <br>
 * t_credits_history <br>
 * v1.0 2016-05-01
 * @see com.ajk.server.dao.CreditsHistoryMapper
 */
 public class CreditsHistoryProvider {
 
 	/**
 	* 新增 积分历史
 	*/
 	public String insert(CreditsHistory domain){
		StringBuilder sb = new StringBuilder();
		
		sb.append("insert into t_credits_history(create_time,last_modify_time");
	 	if(domain.getUserId()!=null){
			sb.append(",user_id");
		}
	 	if(domain.getCategoryId()!=null){
			sb.append(",category_id");
		}
	 	if(domain.getNum()!=null){
			sb.append(",num");
		}
	 	if(domain.getSourceId()!=null){
			sb.append(",source_id");
		}
	 	if(domain.getRemark()!=null){
			sb.append(",remark");
		}
	 	if(domain.getInUsed()!=null){
			sb.append(",in_used");
		}
		
		sb.append(") values ( now(),now() ");
		
	 	if(domain.getUserId()!=null){
			sb.append(",#{userId}");
		}
	 	if(domain.getCategoryId()!=null){
			sb.append(",#{categoryId}");
		}
	 	if(domain.getNum()!=null){
			sb.append(",#{num}");
		}
		if(domain.getSourceId()!=null){
			sb.append(",#{sourceId}");
		}
	 	if(domain.getRemark()!=null){
			sb.append(",#{remark}");
		}
	 	if(domain.getInUsed()!=null){
			sb.append(",#{inUsed}");
		}
		sb.append(")");
		
		return sb.toString();
	}

	/**
 	* 更新 积分历史
 	*/
	public String update(CreditsHistory domain){
		StringBuilder sb = new StringBuilder();
		
		sb.append("update t_credits_history set last_modify_time = now() ");
		
		if(domain.getUserId()!=null){
			sb.append(",user_id=#{userId}");
		}
		if(domain.getCategoryId()!=null){
			sb.append(",category_id=#{categoryId}");
		}
		if(domain.getNum()!=null){
			sb.append(",num=#{num}");
		}
		if(domain.getSourceId()!=null){
			sb.append(",source_id = #{sourceId}");
		}
		if(domain.getRemark()!=null){
			sb.append(",remark=#{remark}");
		}
		if(domain.getInUsed()!=null){
			sb.append(",in_used=#{inUsed}");
		}
		
		sb.append(" where id=#{id}");
		
		return sb.toString();
	}
	
	public String batchInsert(Map<String, List<CreditsHistory>> map){
		StringBuffer sb = new StringBuffer();
		List<CreditsHistory> creditsHistorys = map.get("list");
		sb.append("insert into t_credits_history(create_time,last_modify_time,user_id,category_id,num,source_id,remark,in_used) values");
	    MessageFormat messageFormat = new MessageFormat("("
	    		+ "now(),now()"
	    		+ ",#'{'list[{0}].userId}"
	    		+ ",#'{'list[{0}].categoryId}"
	    		+ ",#'{'list[{0}].num}"
	    		+ ",#'{'list[{0}].sourceId}"
	    		+ ",#'{'list[{0}].remark}"
	    		+ ",#'{'list[{0}].inUsed}"
	    		+")");
	    		
	    if(CollectionUtils.isNotEmpty(creditsHistorys)) {
			sb.append(messageFormat.format(new Integer[]{0}));
			for (int i = 1; i <  creditsHistorys.size(); i++) {
				sb.append(",").append(messageFormat.format(new Integer[]{i}));
			}
		}
		return sb.toString();
	}
	
	/**
 	*通过query 查询 积分历史
 	*/
	public String findByQuery(CreditsHistoryQuery query){
		StringBuffer sb = new StringBuffer();
		sb.append("select * from t_credits_history a where 1 = 1  ");

		if(IntegerUtil.isPositiveInteger(query.getUserId())){
			sb.append(" and user_id=#{userId}");
		}
		if(IntegerUtil.isPositiveInteger(query.getStartId())){
			sb.append(" and id>#{startId} order by id limit #{limit}");
		}else if(IntegerUtil.isPositiveInteger(query.getEndId())){
			sb.append(" and id<#{endId} order by id desc limit #{limit}");
		}else if(query.getStart()!=null && query.getLimit()!=null && query.getLimit()!=0){
			sb.append(" order by id desc limit #{start},#{limit}");
		}else if(query.getLimit()!=null && query.getLimit()!=0){
			sb.append(" order by id desc limit #{limit}");
		}
		return sb.toString();
	}
	
	public String countByQuery(CreditsHistoryQuery query){
		StringBuffer sb = new StringBuffer();
		sb.append("select count(1) from t_credits_history a ");
		return sb.toString();
	}
	
	/**
 	*通过ids 查询 积分历史
 	*/
 	public String findByIds(Map<String, List<Integer>> map){
		StringBuffer sb = new StringBuffer();
		List<Integer> ids = map.get("list");
		sb.append("select * from t_credits_history where ");
		if(ids==null || ids.isEmpty()){
			sb.append(" 1<2");
		}else{
			if(ids.size()==1){
				sb.append(" id=").append("#{list[0]}");
			}else{
				sb.append(" id in (").append("#{list[0]}");
				for(int i=1; i<ids.size() ; i++){
					sb.append(",").append("#{list[").append(i).append("]}");
				}
				sb.append(")");
			}
		}
		return sb.toString();
	}
	
 }	